var express = require("express");
var app = express();

// 静态文件
app.use("/public", express.static("public"));

//#region resetful 风格请求

// 主页
app.get("/", function (req, res) {
  res.send("Hello World");
});

//  POST 请求
app.post("/", function (req, res) {
  console.log("主页 POST 请求");
  res.send("Hello POST");
});

//  /del_user 页面响应
app.get("/del_user", function (req, res) {
  console.log("/del_user 响应 DELETE 请求");
  res.send("删除页面");
});

//  /list_user 页面 GET 请求
app.get("/list_user", function (req, res) {
  console.log("/list_user GET 请求");
  res.send("用户列表页面");
});

// 对页面 abcd, abxcd, ab123cd, 等响应 GET 请求
app.get("/ab*cd", function (req, res) {
  console.log("/ab*cd GET 请求");
  res.send("正则匹配");
});

//#endregion

//#region 页面 和 接受 get 请求

app.get("/get.html", function (req, res) {
  res.sendFile(__dirname + "/views/get.html");
});

app.get("/process_get", function (req, res) {
  // 输出 JSON 格式
  var response = {
    first_name: req.query.first_name,
    last_name: req.query.last_name,
  };
  console.log(response);
  res.end(JSON.stringify(response));
});

//#endregion

//#region 页面 和 接受 post 请求

// 获取其他解析 body 请求
// app.use(express.json());
// app.use(express.urlencoded({ extended: true }));

// text/plain 解析
// const getRawBody = require('raw-body');
// app.post('/receive-text', async (req, res) => {
//   const body = await getRawBody(req);
//   console.log('原始文本内容：', body.toString());
//   res.send(`你发送的内容是：${body}`);
// });

var bodyParser = require("body-parser");

// 创建 application/x-www-form-urlencoded 编码解析
var urlencodedParser = bodyParser.urlencoded({ extended: false });

app.get("/post.html", function (req, res) {
  res.sendFile(__dirname + "/views/post.html");
});

app.post("/process_post", urlencodedParser, function (req, res) {
  // 输出 JSON 格式
  var response = {
    first_name: req.body.first_name,
    last_name: req.body.last_name,
  };
  console.log(response);
  res.end(JSON.stringify(response));
});

//#endregion

//#region 页面 和 上传 文件 请求

app.use(bodyParser.urlencoded({ extended: false }));
var multer = require("multer");
app.use(multer({ dest: "/tmp/" }).array("image"));

app.get("/upload.html", function (req, res) {
  res.sendFile(__dirname + "/views/upload.html");
});

const fs = require("fs");
app.post("/file_upload", function (req, res) {
  console.log(req.files[0]); // 上传的文件信息

  var des_file = __dirname + "/files/" + req.files[0].originalname;
  fs.readFile(req.files[0].path, function (err, data) {
    fs.writeFile(des_file, data, function (err) {
      if (err) {
        console.log(err);
      } else {
        response = {
          message: "File uploaded successfully",
          filename: req.files[0].originalname,
        };
      }
      console.log(response);
      res.end(JSON.stringify(response));
    });
  });
});

//#endregion

//#region cookie 获取

var cookieParser = require("cookie-parser");
app.use(cookieParser());
app.get("/cookie", (req, res) => {
  console.log(req.cookies);

  // 写入 cookie
  res.cookie("name", "tobi", {
    domain: "192.168.0.124",
    path: "/",
    maxAge: 900000,
    httpOnly: true,
    signed: true,
  });
  res.end(JSON.stringify({ cookie: req.cookies }));
});

//#endregion

//#region 启动 express

// agGrid 表格展示
app.get("/index.html", function (req, res) {
  res.sendFile(__dirname + "/views/index.html");
});

const { osHelper } = require("./utils");
// 192.168.0.124(局域网网访问) / 127.0.0.1(本机访问)
var server = app.listen(3001, osHelper.getIpAddress(), function () {
  var host = server.address().address;
  var port = server.address().port;

  console.log("应用实例，访问地址为 http://%s:%s", host, port);
});

//#endregion
